package com.tools.ringtone.edit;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.mp3.dwonloader.ringtonemaker.musicradio.version.zl.R;



public class AudioSaveDialog extends Dialog implements OnClickListener {

	// File kinds - these should correspond to the order in which
	// they're presented in the spinner control
	public static final int FILE_KIND_MUSIC = 0;
	public static final int FILE_KIND_ALARM = 1;
	public static final int FILE_KIND_NOTIFICATION = 2;
	public static final int FILE_KIND_RINGTONE = 3;

	private final Spinner mTypeSpinner;
	private final EditText mFilename;
	private final Message mResponse;
	private final String mOriginalName;
	private final ArrayList<String> mTypeArray;
	private int mPreviousSelection;

	/**
	 * Return a human-readable name for a kind (music, alarm, ringtone, ...).
	 * These won't be displayed on-screen (just in logs) so they shouldn't be
	 * translated.
	 */
	public static String KindToName(int kind) {
		switch (kind) {
		default:
			return "Unknown";
		case FILE_KIND_MUSIC:
			return "Music";
		case FILE_KIND_ALARM:
			return "Alarm";
		case FILE_KIND_NOTIFICATION:
			return "Notification";
		case FILE_KIND_RINGTONE:
			return "Ringtone";
		}
	}

	public AudioSaveDialog(Context context, Resources resources,
			String originalName, Message response) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_save);

		// findViewById(R.id.button_spinner).setOnClickListener(this);

		setTitle(resources.getString(R.string.file_save_title));

		mTypeArray = new ArrayList<String>();
		mTypeArray.add(resources.getString(R.string.type_music));
		mTypeArray.add(resources.getString(R.string.type_alarm));
		mTypeArray.add(resources.getString(R.string.type_notification));
		mTypeArray.add(resources.getString(R.string.type_ringtone));

		mFilename = (EditText) findViewById(R.id.filename);
		mOriginalName = originalName;

		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
				R.layout.spinner_item_type, mTypeArray);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mTypeSpinner = (Spinner) findViewById(R.id.ringtone_type);

		mTypeSpinner.setAdapter(adapter);
		mTypeSpinner.setSelection(FILE_KIND_RINGTONE);
		mPreviousSelection = FILE_KIND_RINGTONE;

		setFilenameEditBoxFromName(false);

		mTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View v,
					int position, long id) {
				setFilenameEditBoxFromName(true);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		final Button save = (Button) findViewById(R.id.save);
		save.setOnClickListener(saveListener);
		final Button cancel = (Button) findViewById(R.id.cancel);
		cancel.setOnClickListener(cancelListener);
		mResponse = response;
	}

	private void setFilenameEditBoxFromName(boolean onlyIfNotEdited) {
		if (onlyIfNotEdited) {
			final CharSequence currentText = mFilename.getText();
			final String expectedText = mOriginalName + " "
					+ mTypeArray.get(mPreviousSelection);

			if (!expectedText.contentEquals(currentText)) {
				return;
			}
		}

		final int newSelection = mTypeSpinner.getSelectedItemPosition();
		final String newSuffix = mTypeArray.get(newSelection);
		mFilename.setText(mOriginalName + " " + newSuffix);
		mPreviousSelection = mTypeSpinner.getSelectedItemPosition();
	}

	private final View.OnClickListener saveListener = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			mResponse.obj = mFilename.getText();
			mResponse.arg1 = mTypeSpinner.getSelectedItemPosition();
			mResponse.sendToTarget();
			dismiss();
		}
	};

	private final View.OnClickListener cancelListener = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			dismiss();
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_spinner:
			break;

		default:
			break;
		}

	}
}
